package com.example.jpetstoreclient.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.jpetstoreclient.common.CommonResponse;
import com.example.jpetstoreclient.domain.User;
import com.example.jpetstoreclient.persistance.UserMapper;
import com.example.jpetstoreclient.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.UUID;

@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    //登录
    @Override
    public CommonResponse<User> login(String username, String password){

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User user = getOne(queryWrapper);

        if(user == null){
            return CommonResponse.createForError("用户名不存在");
        }else if(user.getPassword().equals(getMD5Password(password,user.getSalt()))) {
            return CommonResponse.createForSuccess("登录成功！",user);
        }else {
            return CommonResponse.createForError("密码错误！");
        }
    }

    //注册
    @Override
    public CommonResponse<User> register(User user){
        if(!checkUsername(user.getUsername()))return CommonResponse.createForError("用户名已存在");
        else if(!checkPhone(user.getPhone()))return CommonResponse.createForError("手机号已存在");
        else if(!checkEmail(user.getEmail()))return CommonResponse.createForError("邮箱已存在");
        else {
            encodePassword(user);
            save(user);
            return CommonResponse.createForSuccess("注册成功！",user);
        }
    }

    @Override
    public CommonResponse<User> resetPassword(int user_id,String oldPassword,String newPassword) {
        User user = getById(user_id);
        if(user.getPassword().equals(getMD5Password(oldPassword,user.getSalt()))){
            user.setPassword(newPassword);
            encodePassword(user);
            if(updateById(user))return CommonResponse.createForSuccess("密码修改成功！",user);
            else return CommonResponse.createForError("密码修改失败！");
        }else return CommonResponse.createForError("原密码不正确！");
    }

    @Override
    public CommonResponse<User> getUserById(int id) {
        User user = getById(id);
        if(user == null)return CommonResponse.createForError("用户不存在");
        else return CommonResponse.createForSuccess("查询用户信息",user);
    }

    @Override
    public CommonResponse<User> updateUser(User user) {
        //修改了密码，对其进行加密
        String pwd = user.getPassword();
        if(pwd != null && !pwd.isEmpty()) encodePassword(user);

        if(updateById(user)){
            user = getById(user.getId());
            user.setPassword(null);
            user.setSalt(null);
            return CommonResponse.createForSuccess("用户信息修改成功！",user);
        }else return CommonResponse.createForError("用户信息修改失败！");
    }


    @Override
    public boolean checkUsername(String username){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User user = getOne(queryWrapper);

        return user == null;
    }

    @Override
    public boolean checkPhone(String phone){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone_number",phone);
        User user = getOne(queryWrapper);

        return user == null;
    }

    @Override
    public boolean checkEmail(String email){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email",email);
        User user = getOne(queryWrapper);

        return user == null;
    }

    @Override
    public boolean verifyPassword(int user_id, String password) {
        User user = getById(user_id);
        return user.getPassword().equals(getMD5Password(password,user.getSalt()));
    }

    //加密MD5
    public void encodePassword(User user) {
        String oldPassword = user.getPassword();
        String salt = UUID.randomUUID().toString().toUpperCase();
        user.setSalt(salt);
        String md5Password = getMD5Password(oldPassword, salt);
        user.setPassword(md5Password);
    }


    private String getMD5Password(String password,String salt){
        for (int i = 0;i <3; i++){
            //md5加密算法方法的调用(进行三次加密)
            password = DigestUtils.md5DigestAsHex((salt+password+salt).getBytes()).toUpperCase();
        }
        //返回加密后的密码
        return password;
    }
}
